package com.mywork.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import sun.security.pkcs11.Secmod.Module;

import com.alibaba.fastjson.JSON;
import com.mywork.po.Cureitem_list;
import com.mywork.po.Curelist;
import com.mywork.po.CureprocessView;
import com.mywork.po.Diagnosis;
import com.mywork.po.Drug;
import com.mywork.po.Page;
import com.mywork.po.Patient;
import com.mywork.po.Recipe;
import com.mywork.po.RecipeDiagnosisView;
import com.mywork.po.RecipeSignView;
import com.mywork.po.RecipeView;
import com.mywork.po.Sign;
import com.mywork.po.UseDrugView;
import com.mywork.po.User;
import com.mywork.service.CureListService;
import com.mywork.service.CureitemService;
import com.mywork.service.CureprocessService;
import com.mywork.service.DiagnosisService;
import com.mywork.service.DrugService;
import com.mywork.service.MD5;
import com.mywork.service.MyJSONUtil;
import com.mywork.service.PatientService;
import com.mywork.service.PermissionUtil;
import com.mywork.service.RecipeService;
import com.mywork.service.ShowInformation;
import com.mywork.service.SignService;
import com.mywork.service.UseDrugService;
import com.mywork.service.UserService;


@Controller
@SessionAttributes({"user","permission"})
@RequestMapping("/doctor")
public class DoctorController {
	@Autowired
	CureprocessService cvs;
	@Autowired
	CureitemService cs;
	@Autowired
	UseDrugService uds;
	@Autowired
	PatientService patientService;
	
	@Autowired
	SignService signService;
	
	@Autowired
	CureListService cureService;
	
	@Autowired
	RecipeService recipeService;
	
	@Autowired
	DrugService drugService;
	
	@Autowired
	DiagnosisService diagnosisService;
	/**
	 * 防权限不足
	 * @param request
	 * @param response
	 */
	@ModelAttribute
	public void init(HttpServletRequest request,HttpServletResponse response,Model model,Recipe recipe){
		if(recipe==null){
			model.addAttribute("recipe", recipe);
		}
	}
	/**
	 * 撤销治疗操作
	 * @param cureId
	 * @param cureProcessId
	 * @param recipeId
	 * @param permission
	 * @param user
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/revoke")
	public String revoke(String cureId,String cureProcessId,String recipeId,@ModelAttribute("permission")int permission,@ModelAttribute("user")User user,Model model,HttpServletRequest request){
		if(!PermissionUtil.isHavePermission(user, permission, 2)){
			return "/login";
		}
		
		recipeService.revoke(recipeId,cureId,cureProcessId);
		ShowInformation.forInformation(model, "撤销成功", "返回", request.getContextPath()+"/doctor/showRecipeMessage?recipeId="+recipeId, 4);
		return "information";
		
	}
	
	
	/**
	 * 治疗病人
	 * @param recipeId
	 * @param cureId
	 * @param permission
	 * @param user
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping("/curePatient")
	public String curePatient(String recipeId,String cureId,@ModelAttribute("permission")int permission,@ModelAttribute("user")User user,Model model,HttpServletRequest request){
		if(!PermissionUtil.isHavePermission(user, permission, 2)){
			return "/login";
		}
		
		recipeService.curePatien(recipeId,cureId,user.getUserid());
		ShowInformation.forInformation(model, "治疗成功", "返回", request.getContextPath()+"/doctor/showRecipeMessage?recipeId="+recipeId, 4);
		return "information";
	}
	/**
	 * 查找全部处方
	 * @param model
	 * @return
	 */
	@RequestMapping("/getRecipeList")
	public String getPatientList(String pageNo,Model model,@ModelAttribute("permission")int permission,@ModelAttribute("user")User user){
		if(user==null||permission!=2){
			return "/login";
		}
		Page page = new Page();
		if(pageNo==null||pageNo==""){
			page.setPageNo(0);
		}else{
			page.setPageNo(Integer.valueOf(pageNo).intValue());
		}
		page.setPageSize(10);
		List<RecipeView> recipe_users = recipeService.getAllRecipe(page,user.getUserid());
		model.addAttribute("allPageNo", page.getTotalPage());
		model.addAttribute("recipes", recipe_users);
		return "/prescription/prescriptionList";
	}
	
	//根据病人卡号查看处方
	/**
	 * 根据病人卡号查找处方
	 * @param cardId
	 * @param model
	 * @param request
	 * @param user
	 * @param permission
	 * @return
	 */
	@RequestMapping("/getRecipeByCardId")
	public String getRecipeByCardId(String cardId,Model model,HttpServletRequest request,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission){
		if(!PermissionUtil.isHavePermission(user, permission, 2)){
			return "login";
		}
		if(cardId==null||cardId==""){
			return "/prescription/showPrescription";
		}
		
		List<RecipeView> recipes = recipeService.getRecipeBycardId(cardId,user.getUserid());
		if(recipes.size()==0){
			ShowInformation.forInformation(model, "查无此卡号相关处方信息", "返回", request.getContextPath()+"/doctor/getRecipeByCardId", 1);
			return "information";
		}
		model.addAttribute("recipes", recipes);
		return "/prescription/showPrescription2";
	}
	/**
	 * 根据 病人卡号 添加处方的控制器
	 * 然后添加了一级的药物列表
	 * @param cardId
	 * @param model
	 * @return
	 */
	@RequestMapping("/getPatientByCardId")
	public String getPatientByCardId(HttpServletRequest request,String cardId,Model model,@ModelAttribute("permission")int permission,@ModelAttribute("user")User user){
		if(!PermissionUtil.isHavePermission(user, permission, 2)){
			return "login";
		}
		
		Patient patient=patientService.getPatientByCardId(cardId);
		
		if(patient==null||patient.getCardid()==null||patient.getCardid().trim()==""){
			ShowInformation.forInformation(model, "查无此卡号信息", "返回", request.getContextPath()+"/doctor/addRecipe", 1);
			return "information";
		}
		model.addAttribute("patient", patient);
		List<Drug> level1DrugList = drugService.getAllFirstLevelDrug();
		List<Sign> level1SignList = signService.getAllLevel1Sign();
		List<Diagnosis> level1DiagnosisList = diagnosisService.getAllLevel1Diagnosis();
		List<Curelist> level1Cure = cureService.getCureListByLevel(1);
		model.addAttribute("date",new SimpleDateFormat("yy-MM-dd hh:mm").format(new Date()));
		model.addAttribute("level1DrugList", level1DrugList);
		model.addAttribute("level1DiagnosisList", level1DiagnosisList);
		model.addAttribute("level1SignList", level1SignList);
		model.addAttribute("level1Cure", level1Cure);
		
		
		
		
		
		
		return "/prescription/addPrescription2";
	}

	
	
	/**
	 * 添加处方
	 * @param signId 代表体格诊断，全部input:hidden name = signId
	 * @param signStatue 代表体格诊断属性，全部input:hidden name = signStatue 体格诊断属性（0代表阳性，1代表阴性，代表阴阳性）
	 * @param diagnosisId 代表处方诊断属性，全部input:hidden name = diagnosisId
	 * @param drugId 代表药物Id，全部input:hidden name = drugId
	 * @param cureCount 代表疗程数量，全部input:hidden name = cureCount
	 * @param cureId	代表疗程Id，全部input:hidden name = cureId
	 * @param drugCount 代表药物数量，全部input:hidden name = drugCount
	 * @param recipe
	 * @param request
	 * @param user
	 * @param permission
	 * @return
	 */
	@RequestMapping("/addRecipe")
	public String addRecipe(String[] signId,String[] signStatue,String[] diagnosisId,String[] cureCount,String[] cureId,String[] drugId,String[] drugCount,Recipe recipe,Model model,HttpServletRequest request,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission){
		if(!PermissionUtil.isHavePermission(user, permission, 2)){
			return "/login";
		}
		if(recipe.getPatientid()==null){
			return "/prescription/addPrescription";
		}else{
			recipe.setTime(new Date());
			recipeService.addRecipe(recipe, cureId, cureCount, drugId,drugCount,signId,signStatue,diagnosisId);
			ShowInformation.forInformation(model, "添加成功", "返回", "返回", 4);
			
			return "information";
		}
		
	}
	/**
	 * 查看具体处方信息
	 * @param recipeId
	 * @param model
	 * @param user
	 * @param permission
	 * @param request
	 * @return
	 */
	@RequestMapping("/showRecipeMessage")
	public String showRecipeMessage(String recipeId,Model model,HttpServletRequest request,@ModelAttribute("permission")int permission,@ModelAttribute("user")User user){
		
		if(!PermissionUtil.isHavePermission(user, permission, 2)){
			return "/login";
		}
		RecipeView recipeView=recipeService.getRecipeByRecipeId(recipeId);//获取处方视图（医生名，病人名）
		List<UseDrugView> useDrugView =  uds.getUseDrugByRecipeId(recipeId);//获取用物表列表
		List<Cureitem_list> cureItemView = cs.getCureItemByRecipeId(recipeId);//获取处方治疗项目列表
		List<RecipeSignView> signView = signService.getAllSignByRecipeId(recipeId);//获取处方体格诊断列表
		List<RecipeDiagnosisView> diagnosisView = diagnosisService.getAllDiagnosisByRecipeId(recipeId);//获取处方治疗表
		List<CureprocessView> cureprocessView =  cvs.getCureProcessByRecipeId(Long.valueOf(recipeId).longValue());//获取治疗记录列表
		model.addAttribute("recipeView", recipeView);
		model.addAttribute("useDrugView", useDrugView);
		model.addAttribute("cureItemView", cureItemView);
		model.addAttribute("cureprocessView", cureprocessView);
		model.addAttribute("signView", signView);
		model.addAttribute("diagnosisView",diagnosisView);
		
		
		
		return "/prescription/prescriptionMessage";
	}
	
	/**
	 * 删除处方
	 * @param model
	 * @param recipeId
	 * @param user
	 * @param permission
	 * @param request
	 * @return
	 */
	@RequestMapping("/deleteRecipe")
	public String deleteRecipe(Model model,String recipeId,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,HttpServletRequest request){
		if(!PermissionUtil.isHavePermission(user, permission, 2)){
			return "/login";
		}
		
		recipeService.deleteRecipe(recipeId);
		ShowInformation.forInformation(model, "删除成功","返回", request.getContextPath()+"/doctor/getRecipeList", 4);
		return "information";
	}
}
